package com.yt.website.provider.service.impl;

import com.yt.core.dao.filter.DynamicSQLFilterImpl;
import com.yt.core.dao.filter.QueryFilter;
import com.yt.core.service.GenericServiceImpl;
import com.yt.website.api.interfaces.UserInfoService;
import com.yt.website.api.model.UserInfo;
import com.yt.website.provider.dao.UserInfoDao;

import javax.management.Query;
import java.util.List;

/**
 * Created by liujunshi on 15-12-30.
 */
public class UserInfoServiceImpl extends GenericServiceImpl<UserInfo, Long> implements UserInfoService {
	private UserInfoDao dao;

	public UserInfoServiceImpl() {
	}

	public UserInfoServiceImpl(UserInfoDao dao) {
		super(dao);
		this.dao = dao;
	}

	public boolean checkMobile(String mobile){
		QueryFilter filter = new DynamicSQLFilterImpl();
		filter.addFilter("Q_mobile_S_EQ", mobile);
		int count = dao.count(filter);
		return count == 0;
	}

	public UserInfo getByMobile(String mobile){
		QueryFilter filter = new DynamicSQLFilterImpl();
		filter.addFilter("Q_mobile_S_EQ", mobile);
		List<UserInfo> userInfoList = dao.find(filter);
		if(userInfoList != null && !userInfoList.isEmpty()){
			return userInfoList.get(0);
		}
		return null;
	}

	public UserInfo register(UserInfo userInfo){
		this.insert(userInfo);

		return getByMobile(userInfo.getMobile());
	}
}